package com.bonc.jedis.example;

import java.util.List;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

/**
 * 测试一个jedis对象的时候，事务的情况
 * 测试多个jedis对象的时候，事务的情况
 * @author WYB
 *
 */
public class TransactionExmpl {

	public static void main(String[] args) {
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Jedis jedis = new Jedis(new HostAndPort("192.168.1.136", 6379));
				try {
					long end = System.currentTimeMillis() +10000;
					while (true) {
						jedis.watch("transaction");
						Transaction trans = jedis.multi();
						trans.get("transaction");
						List<Object> result = trans.exec();
						if (result==null) {
							continue;
						}else {
							System.out.println(result);
							break;
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					if (jedis!=null) jedis.close();
				}
				
			}
		}).start();
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				Jedis jedis = new Jedis(new HostAndPort("192.168.1.136", 6379));
				jedis.set("transaction","hello");
//				try {
//					long end = System.currentTimeMillis() +10000;
//					while (System.currentTimeMillis() < end) {
//						Transaction trans = jedis.multi();
//						trans.set("transaction","hello");
//						List<Object> result = trans.exec();
//						if (result==null) {
//							continue;
//						}
//					}
//				} catch (Exception e) {
//					e.printStackTrace();
//				} finally {
//					if (jedis!=null) jedis.close();
//				}
				
			}
		}).start();
	}
}
